package com.jelly.common.util.back;

import com.jelly.common.constant.ShiroConstant;
import com.jelly.common.util.bean.SpringUtil;
import com.jelly.config.redis.session.RedisSessionDAO;
import com.jelly.web.modules.staff.user.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authc.LogoutAware;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;

import java.io.Serializable;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Objects;

/**
 * @创建人 ql
 * @创建时间 2018-08-14 13:14
 * @功能描述: shiro管理工具
 **/
public class ShiroUtil {

    private static Cache<String, Deque<String>> cache = SpringUtil.getBean(CacheManager.class).getCache(ShiroConstant.SHIROREDISCACHE);

    private static String hashAlgorithmName;

    private static int hashIterations;

    public ShiroUtil() {
    }

    /**
     * 删除用户session信息
     * @param loginAccount 用户账号
     */
    public static void delectSession(String loginAccount){
        getCache(loginAccount,"delect");

    }
    /**
     * 更新用户session信息
     * @param loginAccount 用户账号
     */
    public static void updateSession(String loginAccount){
        getCache(loginAccount,"update");
    }
    /**
     * 删除用户缓存信息
     * @param user 用户实体
     */
    public static void kickOutUser(User user){
        Session session = null;//getSessionByUsername(user.getLoginAccount());
        if (session == null) {
            return;
        }

        Object attribute = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        if (attribute == null) {
            return;
        }

        User users = (User) ((SimplePrincipalCollection) attribute).getPrimaryPrincipal();
        if (!user.getLoginAccount().equals(users.getLoginAccount())) {
            return;
        }

        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        Authenticator authc = securityManager.getAuthenticator();
        //删除cache，在访问受限接口时会重新授权
        ((LogoutAware) authc).onLogout((SimplePrincipalCollection) attribute);

    }

    public static String password(User user){
        return new SimpleHash(hashAlgorithmName,user.getPassword(),user.getUserId(),hashIterations).toString();
    }

    protected static void getCache(String loginAccount,String name){
        Deque<String> deque = cache.get("info"+loginAccount);
        if(null == deque){
            deque = new LinkedList<String>();
        }

        if(!deque.contains(name)){
            deque.push(name);
            cache.put("info"+loginAccount,deque);
        }
    }

    public static void setHashAlgorithmName(String hashAlgorithmName) {
        ShiroUtil.hashAlgorithmName = hashAlgorithmName;
    }

    public static void setHashIterations(int hashIterations) {
        ShiroUtil.hashIterations = hashIterations;
    }
}
